package com.china.produce.controller;

import com.alibaba.fastjson.JSONObject;
import com.china.produce.annotation.DBRouter;
import com.china.produce.constat.SecurityConstants;
import com.china.produce.context.SecurityContextHolder;
import com.china.produce.exception.ResultBody;
import com.china.produce.factorys.audio.AliAudioRequestFactory;
import com.china.produce.factorys.audio.AliAudioRequestFactoryBean;
import com.china.produce.factorys.audio.AudioRequestFactory;
import com.china.produce.pojo.User;
import com.china.produce.pojo.user.LoginUser;
import com.china.produce.service.UserService;
import com.china.produce.system.util.JwtUtil;
import com.china.produce.system.util.RedisUtil;
import com.china.produce.task.ITaskTestImpl;
import com.china.produce.vo.UserVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2022/4/2
 */
@Api(tags = "用户模块")
@RequestMapping("/users")
@RestController
@Slf4j
public class UserController implements InitializingBean, ApplicationContextAware {

    @Autowired
    private UserService userService;


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private List<AudioRequestFactory> audioRequestFactoryList;


    @Autowired
    private AliAudioRequestFactoryBean aliAudioRequestFactoryBean;

    private AliAudioRequestFactory factoryBean;

    @Autowired
    @Qualifier("i.task.test.impl")
    private ITaskTestImpl impl;


    @DBRouter(key = "account")
    @ApiOperation("登录")
    @ApiImplicitParam(name = "user", value = "用户实体类", paramType = "body", required = true, dataType = "UserVo")
    @PostMapping("/login")
    public ResultBody login(@RequestBody UserVo user) {

        System.out.println("11111");
        log.info("user : {}", user);
        LoginUser loginUser = new LoginUser();
        loginUser.setName(user.getAccount());
        loginUser.setPwd("123");
        User users = userService.queryUserByName(loginUser);
//        if (Objects.isNull(users)) {
//            return ResultBody.error(LoginUserEnum.ACCOUNT_NOT_EXISTS.getResultMsg());
//        }
//        if (!user.getPassword().equals(users.getPassword())) {
//            log.info("密码错误 !");
//            return ResultBody.error(LoginUserEnum.ACCOUNT_PASSWORD_ERROR.getResultMsg());
//        }
        String token = JwtUtil.sign(user.getAccount(), user.getPassword());
//
//        redisUtil.set(getTokenKey(token), JSONObject.toJSONString(user), 7200, TimeUnit.SECONDS);
        HashMap<String, String> maps = new HashMap<>();
        maps.put("access_token", "123");
        maps.put("expire_time", "7200");
        maps.put("token", token);

        return ResultBody.success(maps);
    }

    private String getTokenKey(String token)
    {
        return "access_token" + token;
    }

    @ApiOperation("校验用户信息")
    @PostMapping("/info")
    @RequiresPermissions("CC3")
    public void info() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        log.info("user: {}", user);

        System.out.println("success !!!");
    }

    @ApiOperation("用户信息")
    @GetMapping("/userInfo")
    public User userInfo() {
        String sUser =  SecurityContextHolder.get(SecurityConstants.USER_KEY);

        return JSONObject.parseObject(sUser, User.class);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        audioRequestFactoryList.forEach(value -> {
            System.out.println(value.getChannelId());
        });
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.factoryBean = (AliAudioRequestFactory) applicationContext.getBean("aliAudioRequestFactoryBean");
    }
}
